<template>
	<view class="container">
		<z-paging ref="paging" refresher-only @onRefresh="onRefresh" @scrolltolower="handleScrollBottom">
			<view class="" slot="top">
				<view class="header">
					<view class="flex align-center justify-between">
						<text class="text-bold f-s28 line-height40">订单</text>
						<view class="p-l16 flex-1 text-right">
							<view class="animated-width" :class="{'show':searchShow}">
								<u-search v-model="queryParams.order_sn" @search="handleSearch" @custom="handleSearch"
									@clear="handleSearch" @blur="handleSearchBlur" :focus="searchShow"
									placeholder="请输入订单编号搜索" placeholderColor="#B2B8C1" bgColor="#F0F1F2"
									:animation="true" searchIcon="/static/images/mine/search.png"
									style="font-size: 30rpx;" class="full-width"></u-search>
							</view>
						</view>

						<view class="" @click="searchShow = true" v-show="!searchShow">
							<image src="/static/images/mine/search.png" class="width-22 height-22" mode="">
							</image>
						</view>

					</view>
					<view class="tag">
						<view class="tag-item" v-for="(item,index) in tagList" :key="index"
							@click="handleTagChange(index,item)" :class="{'tag-active':index === tagCurrent}">
							<text>{{item.text}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="card">
				<view class="card-item" @click="handleToPage(item)" v-for="(item,index) in list" :key="item.id">
					<view class="flex justify-between align-center">
						<view class="flex align-center flex-1 overflow-hidden">
							<image :src="item.avatar || '/static/logo.png'"
								class="round width-28 height-28 m-r10 bg-gray shrink-0" mode=""></image>
							<text class="m-r3 f-s12 f-w500 line-height16 text-black text-cut">{{item.nickname}}</text>
							<image src="/static/images/signup/vip.png" class="width-22 height-14 shrink-0" mode=""
								@click.stop="handleClickVip" v-if="item.is_vip && item.is_vip > 0"></image>
							<text class="text-gray f-s12 f-w400 m-l8 shrink-0">{{item.appointment_time}}</text>
							<view class="clock-tag m-l5 shrink-0" v-if="item.clock_num && item.clock_num > 0">
								<text>加时</text>
							</view>
							<view class="clock-tag m-l5 shrink-0" v-if="item.upgrade_order_count && item.upgrade_order_count > 0">
								<text>升级</text>
							</view>
						</view>
						<view class="shrink-0 flex align-center m-l5">
							<text class="text-theme f-s12 f-w400">{{item.status_text}}</text>
						</view>
					</view>
					<view class="flex full-width m-t10">
						<image :src="item.cover || '/static/logo.png'" class="cover" mode=""></image>
						<view class="m-l12 full-width flex flex-direction justify-between">
							<view class="flex justify-between align-center">
								<view class="flex align-center">
									<text class="text-bold f-s15 line-height22 m-r4">{{item.service_name}}</text>
								</view>
							</view>
							<view class="m-t8 flex flex-wrap">
								<!-- <text class="text-gray f-s12">{{item.remark}}</text> -->
								<view class="card-item-tag"
									v-for="tag in item.remark?item.remark.split(','):[]" :key="tag">
									<text>{{tag}}</text>
								</view>
							</view>
							<view class="">
								<text class="f-s14 text-bold text-theme line-height18">￥{{item.pay_money}}</text>
								<!-- <text class="text-gray f-s12"></text> -->
								<text class="p-l8 text-gray f-s12">时长{{formatTime(item.duration)}}</text>
							</view>
						</view>
					</view>
					<view class="m-t14 flex justify-between p-lr12 p-tb8"
						style="background-color: #F5F6F7;border-radius: 8rpx;">
						<view class="flex align-baseline basis-df">
							<text class="f-s12 text-black">出行费用(出租)</text>
							<text class="m-l6 text-theme text-bold f-s14 line-height20">¥{{item.fare}}</text>
						</view>
						<view class="flex align-baseline basis-df">
							<text class="f-s12 text-black">项目收益</text>
							<text
								class="m-l6 text-theme text-bold f-s14 line-height20">¥{{item.technician_money}}</text>
						</view>
					</view>
				</view>
				<view class="padding-tb-xs" v-if="total !== 0">
					<u-loadmore :status="loadmoreStatus" line @loadmore="handleScrollBottom" />
				</view>
				<Empty :show="total === 0"></Empty>
			</view>
		</z-paging>
		<Toast></Toast>
	</view>
</template>

<script>
	import Empty from '@/components/empty/index.vue';
	import mixins from "@/mixins";
	import {
		list
	} from "@/api/order"
	export default {
		components: {
			Empty
		},
		mixins: [mixins],
		data() {
			return {
				searchShow: false,
				tagCurrent: 0,
				list: [],
				queryParams: {
					order_sn: '',
					order_status: '',
					limit: 10,
					page: 1
				},
				tagList: [{
					text: "全部",
					value: ''
				}, {
					text: "待接单",
					value: '1'
				}, {
					text: "待服务",
					value: '2'
				}, {
					text: "进行中",
					value: '3'
				}, {
					text: "已完成",
					value: '4'
				}, {
					text: "待评价",
					value: '5'
				},{
					text: "退款",
					value: '7'
				}]
			}
		},
		onShow() {
			if (this.queryParams.page <= 1) {
				this.getList()
			}
		},
		onHide() {
			this.queryParams.order_sn = ''
			this.searchShow = false
		},
		methods: {
			formatTime(minutes) {
				if (minutes < 60) {
					// 如果未超过60分钟，直接显示分钟
					return `${minutes}分钟`;
				} else {
					// 如果超过60分钟，转换为 "时:分" 格式
					const hours = Math.floor(minutes / 60); // 计算小时
					const remainingMinutes = minutes % 60; // 计算剩余的分钟
					if (remainingMinutes == 0) {
						return `${hours}小时`;
					}
					return `${hours}小时${remainingMinutes}分钟`;
				}
			},
			// 下拉刷新被触发
			onRefresh() {
				this.queryParams.page = 1
				this.getList('scroll')
			},
			handleSearch() {
				this.queryParams.page = 1
				this.getList()
			},
			handleSearchBlur() {
				// this.queryParams.order_sn = ''
				// this.searchShow = false
			},
			getList(type) {
				this.loadmoreStatus = 'loading'
				list(this.queryParams).then(res => {
					if (type === 'scroll') {
						this.$refs.paging.complete();
					}
					let {
						data: {
							list,
							count,
							total
						}
					} = res
					this.total = count
					this.pageTotal = total
					if (this.queryParams.page === this.pageTotal) this.loadmoreStatus = 'nomore'
					if (this.queryParams.page === 1) {
						this.list = list
					} else if (this.queryParams.page < this.pageTotal) {
						this.list = this.list.concat(list)
					}
				}).catch(() => {
					if (type === 'scroll') {
						this.$refs.paging.complete();
					}
				})
			},
			handleTagChange(index, select) {
				if (this.tagCurrent === index) return
				this.tagCurrent = index
				this.queryParams.order_status = select['value']
				this.queryParams.page = 1
				this.getList()
			},
			handleClickVip() {
				this.$toast('VIP会员用户')
			},
			handleToPage(select) {
				this.$tab.navigateTo('detail?id=' + select.order_sn)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: inherit;

		.header {
			background-color: inherit;
			z-index: 99;
			position: sticky;
			top: 0;
			padding: 0 24rpx 10rpx 24rpx;
			padding-top: calc(var(--status-bar-height) + 36rpx);
		}

		.tag {
			margin: 24rpx 0;
			display: flex;
			overflow-y: auto;

			.tag-item {
				padding: 16rpx 32rpx;
				background-color: #FFFFFF;
				border-radius: 32rpx;
				font-weight: 400;
				font-size: 24rpx;
				text-align: center;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.tag-active {
				background-color: #202020;
				color: #fff;
			}
		}

		.card {
			padding: 0 24rpx 36rpx 24rpx;

			.card-item {
				border-radius: 20rpx;
				padding: 20rpx;
				background-color: #fff;
				margin-bottom: 20rpx;

				.cover {
					width: 176rpx;
					height: 176rpx;
					background: #D8D8D8;
					border-radius: 16rpx;
					flex-shrink: 0;
				}

				.card-item-tag {
					border-radius: 4rpx;
					border: 1rpx solid #FE3F59;
					font-size: 22rpx;
					color: #FE3F59;
					line-height: 32rpx;
					padding: 0 6rpx;
					margin-right: 20rpx;
					margin-bottom: 10rpx;
				}
				.clock-tag{
					border-radius: 4rpx;
					background-color: #FE3F59;
					font-size: 22rpx;
					color: #fff;
					line-height: 32rpx;
					padding: 0 6rpx;
				}
				.card-item-btn {
					// width: 168rpx;
					padding: 6rpx 16rpx;
					background: #222222;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 22rpx;
					color: #FFFFFF;
					line-height: 32rpx;
					text-align: center;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
				}
			}
		}
	}
	
	.animated-width {
		width: 0;
		overflow: hidden;
		transition: width 0.5s ease-in-out;
		transform-origin: right;
		/* 设置变换原点为右侧 */
		display: inline-block;
	}

	.animated-width.show {
		width: 100%;
	}
</style>